home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / bc / pro7 / fcopy.c < prev    next >
C/C++ Source or Header  |  1993-11-25  |  6KB  |  142 lines

  1. /*--------------------------------------------------------------------------
  2.  * filecopy.c
  3.  *
  4.  * Original Code by Ed Mulroy 1992
  5.  * Modified by Al Gifford 1993
  6.  *
  7.  * Original code was written to demonstrate a file copy from inside a C
  8.  * program.  This code is a function which can be easily included into your
  9.  * code to allow you to copy files from one directory to another.
  10.  *
  11.  * If you get errors indicating there is not enough memory, you can either
  12.  * decrease the size of the copy buffer, or you can increase the size of
  13.  * your memory model.
  14.  *
  15.  * There are still many changes which could be made to this function.  Feel
  16.  * free to do so.
  17.  *
  18.  * Version 1.10 - added some features that users requested
  19.  *              - error checking on reads & writes
  20.  *              - setting DOS' verify flag on
  21.  *              - reporting DOS error messages
  22.  *              - writes are of the number actually read
  23.  *                rather than the number requested to be read
  24.  *
  25.  * Version 1.20 - Changed copy program example to a function call.
  26.  *              - free'd copy buffer after function call.
  27.  *              - modified new file's time and date stamp to accurately
  28.  *                reflect the time and date stamp of the original file.
  29.  *
  30.  * permission granted for any private or commercial use
  31.  *
  32.  */
  33.  
  34. #include <stdio.h>                 /* fputs(), putchar(), perror(), _fmode */
  35. #include <stdlib.h>                            /* malloc(), free(), exit() */
  36. #include <sys\stat.h>                                          /* S_IWRITE */
  37. #include <fcntl.h>                                             /* O_BINARY */
  38. #include <dos.h>                               /* setverify(), getverify() */
  39. #include <io.h>                       /* open(), creat(), read(), write(),
  40.                      close(), access(), filelength()   */
  41.  
  42. #define BUF_SIZE (32 * 1024U)      /* copy buffer size, DOS limit is < 64K */
  43.  
  44. int save_verify;                  /* save the old value of the verify flag */
  45.  
  46. char *logo = "Filecopy v1.20\n";
  47. char *read_error_msge = "Read file error";
  48. char *write_error_msge = "Write file error";
  49.  
  50. void fatal(char *msge)                   /* give an error message and quit */
  51.    {
  52.    fputs(msge,stderr);       /* write to stderr so it can't be re-directed */
  53.    setverify(save_verify);                      /* restore old verify flag */
  54.    exit(2);               /* return 2 as some systems return 1 for success */
  55.    }
  56.  
  57. void file_error(char *msge)       /* give our & DOS error message and quit */
  58.    {
  59.    perror(msge);                       /* give our and DOS' error messages */
  60.    setverify(save_verify);                      /* restore old verify flag */
  61.    exit(2);
  62.    }
  63.  
  64. int fcopy(char *file1,char *file2)
  65.    {
  66.    long     numleft;                  /* number of bytes remaining to copy */
  67.    unsigned numnow;                       /* number of bytes for this pass */
  68.    unsigned numxfer;           /* number of bytes actually read or written */
  69.    int      inhand;                                   /* input file handle */
  70.    int      outhand;                                 /* output file handle */
  71.    char    *buffer;           /* pointer which will be copy buffer address */
  72.    char     ch;
  73.    struct ftime ft;
  74.  
  75.    fputs(logo,stderr);                                /* show program name */
  76.    save_verify = getverify();
  77.    setverify(1);                              /* set DOS' verify file flag */
  78.  
  79.    if((buffer = (char *)malloc(BUF_SIZE)) == NULL) /* allocate copy buffer */
  80.       fatal("Not enough memory to run program\n");
  81.  
  82.    if((inhand = open(file1,O_RDONLY|O_BINARY)) == -1)
  83.       file_error("Error opening source file");
  84.  
  85.    if(!access(file2,0))             /* test for destination file existence */
  86.       {
  87.       fputs( "Destination file exists.  Overwrite? [Y/N] ", stderr);
  88.       fflush(stdin);
  89.       ch = getchar();
  90.  
  91.       if((ch != 'y') && (ch != 'Y'))
  92.      fatal("\nCopying aborted\n");
  93.       }
  94.  
  95.    _fmode = O_BINARY;                 /* creat() uses _fmode for file mode */
  96.  
  97.    if((outhand = creat(file2, S_IWRITE)) == -1)
  98.       file_error("Error creating destination file");
  99.  
  100.    numleft = filelength(inhand);                   /* get source file size */
  101.    fputs("copying ",stdout);
  102.  
  103.    while(numleft > 0)
  104.       {
  105.       putchar('.');                                       /* show activity */
  106.  
  107.       if(numleft > (long) BUF_SIZE)       /* decide on this pass copy size */
  108.      numnow = BUF_SIZE;
  109.       else
  110.      numnow = (unsigned) numleft;
  111.  
  112.       numxfer = read(inhand, buffer, numnow);
  113.  
  114.       if(numxfer == (unsigned) -1)            /* if a DOS error on reading */
  115.      file_error(read_error_msge);
  116.       else if(numxfer == 0)           /* if 0, DOS doesn't report an error */
  117.      fatal(read_error_msge);
  118.  
  119.       numxfer = write(outhand,buffer,numxfer);
  120.  
  121.       if(numxfer == (unsigned) -1)            /* if a DOS error on writing */
  122.      file_error(write_error_msge);
  123.       else if(numxfer == 0)           /* if 0, DOS doesn't report an error */
  124.      fatal(write_error_msge);
  125.  
  126.       numleft -= numxfer;                 /* decrement amount left to copy */
  127.       }
  128.  
  129.    getftime(inhand,&ft);                              /* get time and date */
  130.    setftime(outhand,&ft);                             /* set time and date */
  131.  
  132.    if(close(inhand) == -1)
  133.       file_error("Input file close error");
  134.  
  135.    if(close(outhand) == -1)
  136.       file_error("Output file close error");
  137.  
  138.    free(buffer);
  139.    puts(" Done");
  140.    return 0;
  141.    }
  142.